package cn.kgc.kongqi.controller;

import cn.kgc.kongqi.pojo.Index;
import cn.kgc.kongqi.pojo.ResultEntity;
import cn.kgc.kongqi.service.IndexService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-20 23:14
 */
@RestController
public class IndexController {

    @Resource
    IndexService indexService;

    @RequestMapping("/selectPageInfo")
    public ResultEntity<PageInfo<Index>> selectPageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSzie", defaultValue = "3", required = false) Integer pageSize,
            @RequestParam(value = "districtId", defaultValue = "0", required = false) Integer districtId
    ) {
        PageInfo<Index> pageInfo = indexService.selectPageInfo(pageNum, pageSize, districtId);
        return ResultEntity.successWithoutData(pageInfo);
    }


    @RequestMapping("/addIndex")
    public ResultEntity<String> addIndex(@RequestBody Index index) {
        try {
            Integer integer = 0;
            if (index.getId() == null) {
                indexService.addIndex(index);
            } else {
                indexService.updateIndex(index);
            }
            return ResultEntity.successWithoutData(integer + "");
        } catch (Exception e) {
            return ResultEntity.failed(e.getMessage());
        }

    }

    //删除
    @RequestMapping("/deleteIndex")
    public ResultEntity<String> deleteIndex(Integer id) {
        try {//删除成功
            Integer integer = indexService.deleteIndex(id);
            return ResultEntity.successWithoutData(integer + "");
        } catch (Exception e) {//删除失败
            return ResultEntity.failed(e.getMessage());
        }
    }

    //根据id查询
    @RequestMapping("/IndexById")
    public ResultEntity<Index> indexById(Integer id) {
        Index index = indexService.indexById(id);
        return ResultEntity.successWithoutData(index);
    }


}
